cmake_minimum_required (VERSION 3.12)

add_compile_definitions(UNICODE _UNICODE)
if (WIN7_SYSTEM_SUPPORT MATCHES ON)
	add_compile_definitions(_WIN32_WINNT=0x601)
endif()

add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

aux_source_directory(. DIR_SRCS)
file(GLOB HLSL_FILES HLSL/*.hlsl)
file(GLOB HEADER_FILES ./*.h)

foreach(HLSL_FILE ${HLSL_FILES})

	get_filename_component(HLSL_FDIR ${HLSL_FILE} DIRECTORY)
	get_filename_component(HLSL_FBASENAME_WE ${HLSL_FILE} NAME_WE)
	string(CONCAT HLSL_FNAME_WE ${HLSL_FDIR} / ${HLSL_FBASENAME_WE})

	string(LENGTH ${HLSL_FBASENAME_WE} LEN_FNAME_WE)
	
	math(EXPR LEN_FNAME_WE "${LEN_FNAME_WE}-2")
	string(SUBSTRING ${HLSL_FBASENAME_WE} ${LEN_FNAME_WE} 2 ENTRY_POINT)
	string(TOLOWER ${ENTRY_POINT} SHADER_TYPE)

	if ("${SHADER_TYPE}" STREQUAL "vs") 
		set(SHADER_TYPE "Vertex")
	elseif("${SHADER_TYPE}" STREQUAL "hs") 
		set(SHADER_TYPE "Hull")
	elseif("${SHADER_TYPE}" STREQUAL "ds") 
		set(SHADER_TYPE "Domain")
	elseif("${SHADER_TYPE}" STREQUAL "gs") 
		set(SHADER_TYPE "Geometry")
	elseif("${SHADER_TYPE}" STREQUAL "ps")
		set(SHADER_TYPE "Pixel")
	elseif("${SHADER_TYPE}" STREQUAL "cs")
		set(SHADER_TYPE "Compute")
	endif()
	set_source_files_properties(${HLSL_FILE} PROPERTIES
								VS_SHADER_OBJECT_FILE_NAME HLSL/${HLSL_FBASENAME_WE}.cso
								VS_SHADER_TYPE ${SHADER_TYPE}
								VS_SHADER_MODEL 5.0
								VS_SHADER_ENTRYPOINT ${ENTRY_POINT}
								VS_SHADER_DISABLE_OPTIMIZATIONS $<$<CONFIG:Debug>:true>
								VS_SHADER_ENABLE_DEBUG $<$<CONFIG:Debug>:true>)
endforeach()

add_executable(02_Rendering_a_Triangle WIN32 ${DIR_SRCS} ${HEADER_FILES} ${HLSL_FILES})
source_group("HLSL Files" FILES ${HLSL_FILES})
set_target_properties(02_Rendering_a_Triangle PROPERTIES OUTPUT_NAME "02 Rendering a Triangle")
target_link_libraries(02_Rendering_a_Triangle d3d11.lib dxgi.lib dxguid.lib D3DCompiler.lib d2d1.lib dwrite.lib winmm.lib)

set_target_properties(02_Rendering_a_Triangle PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_BINARY_DIR})
set_target_properties(02_Rendering_a_Triangle PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_BINARY_DIR})
set_target_properties(02_Rendering_a_Triangle PROPERTIES FOLDER "Project 01-09")
